package com.lwb.demo1.service;

import com.lwb.demo.api.DemoService2;
import com.lwb.demo.api.DemoService3;
import com.lwb.demo.entity.DemoEntity;
import com.lwb.demo1.dao.Demo1Dao;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Demo1Service {

    @Resource
    Demo1Dao demo1Dao;

    @DubboReference
    DemoService2 demoService2;

    @DubboReference
    DemoService3 demoService3;


    public List<DemoEntity> selectById(String id) {
        return demo1Dao.selectById(id);
    }

    @GlobalTransactional(name = "demo1", rollbackFor = RuntimeException.class)
    public void save(String id) {
        System.out.println("全局事务id ：" + RootContext.getXID());
//        DemoEntity de = new DemoEntity();
//        de.setContext("id:" + id);
//        de.setId(id);
//        demo1Dao.save(de);
        demoService2.execute(id);
        demoService3.execute(id);
        throw new RuntimeException("测试抛异常后，分布式事务回滚！");
    }
}
